<?php
namespace Home\Controller;

class CustomerController extends ParentController {
    protected function _initialize() {
        $this->hasRight = parent::isLogin();
    }

    /**
     * 获取用户个人会员卡信息
     * @modify wenhui 2018-01-17    创建
     */
    public function hasUserVipCard() {
        if (empty($this->hasRight)) {
            returnJson(403, 304);
            return;
        }

        $hotelId = I('post.hotel');
        $areaCode = session(C('USER_AREACODE'));
        $mobile = session(C('USER_MOBILE'));
        // 未登录以及微信登录并且未绑定手机号
        $wechatAndNotBindMobile = session(C('AUTH_LOGIN_TYPE')) == USER_OAUTH_TYPE_WECHAT_WEB && empty($mobile);
        if ($wechatAndNotBindMobile) {
            returnJson(200, 200, array());
            return;
        }


        $hotelDetailModel = D('HotelDetail');
        $vipUserModel = D('VipUser');
        $vipUserInfo = $vipUserModel->getUserInfoByMobile($mobile, $areaCode);
        if (empty($vipUserInfo)) {
            returnJson(200, 200, array());
            return;
        }

        $hotelIdArray = array_column($vipUserInfo, 'hotelentity_id');
        $vipUserInfo = getArrayValueAsIndex($vipUserInfo, 'hotelentity_id');
        // 获取已开通微预定 并且未过期客栈
        $hotelDetail = $hotelDetailModel->getHotelInfo($hotelIdArray);

        $returnData = array();
        $firstArray = array();
        foreach ($hotelDetail as $key => $hotelDetail) {
            if ($hotelDetail['hotelentity_id'] == $hotelId) {
                $firstArray = array(
                    'hotelId' => $hotelDetail['hotelentity_id'],
                    'vipInfoId' => $vipUserInfo[$hotelDetail['hotelentity_id']]['vipinfo_id'],
                    'vipUserId' => $vipUserInfo[$hotelDetail['hotelentity_id']]['id'],
                    'name' => $hotelDetail['name'],
                    'logo' => !empty($hotelDetail['logoimageurl']) ?  MEIZHU_HOST . $hotelDetail['logoimageurl'] : '',
                ); 
            } else {
                $returnData[] = array(
                    'hotelId' => $hotelDetail['hotelentity_id'],
                    'vipInfoId' => $vipUserInfo[$hotelDetail['hotelentity_id']]['vipinfo_id'],
                    'vipUserId' => $vipUserInfo[$hotelDetail['hotelentity_id']]['id'],
                    'name' => $hotelDetail['name'],
                    'logo' => !empty($hotelDetail['logoimageurl']) ?  MEIZHU_HOST . $hotelDetail['logoimageurl'] : '',
                ); 
            }
        }
        unset($vipUserItem);

        if (!empty($firstArray)) {
            array_unshift($returnData, $firstArray);
        }

        returnJson(200, 200, $returnData);
        return;
    }

    /**
     * 获取用户个人会员卡信息
     * @modify wenhui 2018-01-17    创建
     */
    public function hasUserVipCardDetial() {
        if (empty($this->hasRight)) {
            returnJson(403, 304);
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'vipInfoId' => 'notnull,notempty,id',
            'vipUserId' => 'notnull,notempty,id',
        );

        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        $hotel = $param['hotel'];

        $hotelDetailModel = D('HotelDetail');
        // 获取已开通微预定 并且未过期客栈
        $hotelDetail = $hotelDetailModel->getHotelInfo($hotel);

        Vendor('phpRPC.phprpc_client');
        // 获取客栈支付信息
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $hotel,
        );
        $payInfoResult = $client->payInfo($RPCData);
        if (!is_array($payInfoResult)) {
            returnJson(403, 101, '500,500', null, null, '服务器繁忙,请稍后重试');
            return;
        }
        if ($payInfoResult['httpstatus'] != 200) {
            returnJson(403, 101, $payInfoResult['httpstatus'] . $payInfoResult['status'], null, null, $payInfoResult['info']);
            return;
        }
        $payLogic = A('Common/Pay', 'Logic');
        $payTypeStatus = $payLogic->getHotelPayType($hotel, (int)$payInfoResult['data']['type']);
        $ali = $payTypeStatus['hotel_alipay'] ? ENABLE : DISABLE;
        $wechat = $payTypeStatus['hotel_wechat'] ? ENABLE : DISABLE;
        // 获取客栈默认货币
        $defaultCurrency = $this->assignHotelDefultCurrency($hotel, true);
        $isCharge = true;
        // 只有人民币才可以充值 8 = 人民币
        if (!empty($defaultCurrency) && $defaultCurrency['currency_id'] != 8) {
            $isCharge = false;
        }
        // 获取会员卡信息
        $client = new \PHPRPC_Client(C('CUSTOMER_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotelId' => $hotel,
            'vipInfoId' => $param['vipInfoId'],
            'vipUserId' => $param['vipUserId'],
        );

        $result = $client->getVipCardDetial($RPCData);

        if ($result['httpstatus'] == 200) {
            $returnData = $result['data'];
            // 处理数据
            $returnData['bookUrl'] = MEIZHU_BOOK_HOST . '/Home/BookPage/index?hotel=' . $hotel . '&name=Bind';
            $returnData['mobile'] = '+' . $returnData['areaCode'] . ' ' . $returnData['mobile'];
            $returnData['ali'] = $ali;
            $returnData['wechat'] = $wechat;
            $returnData['isCharge'] = $isCharge;
            $returnData['vipUserId'] = $returnData['id'];
            $returnData['vipInfoId'] = $returnData['vipinfo_id'];
            $returnData['hotelName'] = $hotelDetail[0]['name'];
            $returnData['logo'] = !empty($hotelDetail[0]['logoimageurl']) ?  MEIZHU_HOST . $hotelDetail[0]['logoimageurl'] : '';
            foreach ($returnData['hotelVipInfo'] as &$vipInfo) {
                if ($returnData['vipinfo_id'] == $vipInfo['id']) {
                    $returnData['currentDiscount'] = $vipInfo['discount'];
                    $returnData['currentDiscountrule'] = $vipInfo['discountrule'];
                }

                unset($vipInfo['id']);
                unset($vipInfo['hotelentity_id']);
                unset($vipInfo['auto']);
            }
            unset($vipInfo);

            unset($returnData['areaCode']);
            unset($returnData['id']);
            unset($returnData['vipinfo_id']);
            unset($returnData['hotelentity_id']);

            returnJson(200, 200, $returnData);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取用户个人会员卡信息
     * @modify wenhui 2018-01-17    创建
     */
    public function getUserVipCardRecord() {
        if (empty($this->hasRight)) {
            returnJson(403, 304);
            return;
        }

        $param = I('post.');
        $validateRule = array(
            'hotelId' => 'notnull,notempty,id',
            'vipUserId' => 'notnull,notempty,id',
        );

        try {
            \Org\Util\Validate::validate($param, $validateRule);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        // 获取会员卡信息
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('CUSTOMER_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotelId' => $param['hotelId'],
            'vipUserId' => $param['vipUserId'],
            'langSet' => LANG_SET,
        );

        $result = $client->getUserVipCardRecord($RPCData);

        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']['recordInfo']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取支付信息
     * @modify wenhui 2018-01-17    创建
     */
    public function getPayInfo() {
        if (empty($this->hasRight)) {
            returnJson(403, 304);
            return;
        }

        $params = I('post.');

        $hotelId = $params['hotelId'];
        $vipUserId = $params['vipUserId'];

        $validateRule = array(
            'hotelId' => 'notnull,notempty,id',
            'vipUserId' => 'notnull,notempty,id',
        );

        try {
            \Org\Util\Validate::validate($params, $validateRule);
        } catch (\Exception $e) {
            returnJson($e->getMessage());
            return;
        }

        Vendor('phpRPC.phprpc_client');
        // 获取客栈支付信息
        $client = new \PHPRPC_Client(C('HOTEL_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $hotelId,
        );
        $payInfoResult = $client->payInfo($RPCData);
        if (!is_array($payInfoResult)) {
            returnJson(403, 101, '500,500', null, null, '服务器繁忙,请稍后重试');
            return;
        }
        if ($payInfoResult['httpstatus'] != 200) {
            returnJson(403, 101, $payInfoResult['httpstatus'] . $payInfoResult['status'], null, null, $payInfoResult['info']);
            return;
        }
        $payLogic = A('Common/Pay', 'Logic');
        $payTypeStatus = $payLogic->getHotelPayType($hotelId, (int)$payInfoResult['data']['type']);

        $hotelDetailModel = D('HotelDetail');
        $vipUserCardModel = D('VipUserCard');
        // 获取已开通微预定 并且未过期客栈
        $hotelDetail = $hotelDetailModel->getHotelInfo($hotelId);
        $vipUserCardInfo = $vipUserCardModel->getInfoByVipUserId($vipUserId);

        $returnData['hotelName'] = $hotelDetail[0]['name'];
        $returnData['cardPrice'] = isset($vipUserCardInfo['price']) ? $vipUserCardInfo['price'] : 0;
        $returnData['alipay'] = $payTypeStatus['hotel_alipay'] ? 1 :0;
        $returnData['wechatpay'] = $payTypeStatus['hotel_wechat'] ? 1 :0;

        returnJson(200, 200, $returnData);
        return;
    }
}
